const zbTool = function () {
    this.isArray = function (o){
        return Object.prototype.toString.call(o) === '[object Array]';
    }

    //查找元素在二维数组中的索引值
    this.arraySearch = function (arr, key, val) {
        for (let i=0; i<arr.length; i++) {
            if (arr[i][key] !== 'undefined' && arr[i][key] === val) {
                return arr[i];
            }
        }
        return false;
    }

    //获取随机数
    this.getRandom = function (len) {
        let char = '';
        for (let i=0; i<len; i++) {
            char += Math.floor(Math.random()*10);
        }

        return char;
    }

    this.getYear = function () {
        let myDate = new Date();
        return myDate.getFullYear();
    }

    this.getMonth = function () {
        let myDate = new Date();
        return myDate.getMonth() + 1;
    }

    this.getDay = function () {
        let myDate = new Date();
        return myDate.getDate();
    }

    this.getToday = function (gap='-') {
        let d = new Date();
        d.setTime(d.getTime());
        let year = d.getFullYear();
        let month = d.getMonth()+1;
        let day = d.getDate();
        month = strPad(month.toString(),'0', 2 );
        day = strPad(day.toString(), '0', 2);
        return year + gap + month + gap + day;
    }

    //给字符串str补上前导字符pad, 最终使str总长为length
    this.strPad = function (str, pad, length) {
        var padn = length - str.length;
        var pads = '';

        for (i =0; i < padn ; i++) {
            pads += pad;
        }

        return pads + str;
    }

    //将字符串拆分成键值对
    this.explodeArgs = function (str, char1, char2) {
        let arr = str.split(char1);
        let list = {};
        for (let i = 0; i < arr.length; i++) {
            let tmp = arr[i].split(char2);
            list[tmp[0]] = tmp[1];
        }

        return list;
    }

    //滚动到顶部
    this.goScrollTop = function () {
        //把内容滚动指定的像素数（第一个参数是向右滚动的像素数，第二个参数是向下滚动的像素数）
        //向上是负数，向下是正数
        window.scrollBy(0, -100);
        //延时递归调用，模拟滚动向上效果
        scrolldelay = setTimeout('goScrollTop()', 30);
        //获取scrollTop值，声明了DTD的标准网页取document.documentElement.scrollTop，
        //否则取document.body.scrollTop；因为二者只有一个会生效，另一个就恒为0，所以取和值可以得到网页的真正的scrollTop值
        let sTop = document.documentElement.scrollTop + document.body.scrollTop;
        //判断当页面到达顶部，取消延时代码（否则页面滚动到顶部会无法再向下正常浏览页面）
        if (sTop <= 0) {
            clearTimeout(scrolldelay);
        }
    }

    //获取页面文档的总高度
    this.documentHeight = function (){
        //现代浏览器（IE9+和其他浏览器）和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
        return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
    }

    //获取窗口顶部已遮盖的的文档高度
    this.scrollTop = function () {
        let len = Math.max(
            //chrome
            document.body.scrollTop,
            //firefox/IE
            document.documentElement.scrollTop);

        return Math.round(len);
    }

    //获取窗口的高度
    this.windowHeight = function (){
        return (document.compatMode == "CSS1Compat")
            ? document.documentElement.clientHeight
            : document.body.clientHeight;
    }

    //是否已经滑倒了底部
    this.isScrollBottom = function (offset = 50) {
        let total_doc_len = this.documentHeight(); //文档总长度
        let already_scroll_len = this.scrollTop() + this.windowHeight(); //已经显示的长度
        let diff = total_doc_len - already_scroll_len;

        if( diff < offset) { //两者之差小于offset时， 说明已经快到底了
            return true;
        } else {
            return false;
        }
    }
}